package servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

public class CheckLogin extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies = request.getCookies();
        String username = null;
        String password = null;
        for(Cookie ck : cookies) {
            if(ck.getName().equals("username")) {
                username = ck.getValue();
            }
            if(ck.getName().equals("password")) {
                password = ck.getValue();
            }
        }
        // avoid create NullPointer.
        if(Objects.equals(username, "China") && Objects.equals(password, "123")) {
            response.sendRedirect("./welcome.jsp");
        } else {
            response.sendRedirect("./Login.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}


















